package com.cyz.web.app.thread.atomic;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.LongAdder;

@Slf4j
public class T {

    AtomicInteger count2 = new AtomicInteger(2);

    int count = 0;


    LongAdder count3 = new LongAdder();

    synchronized void m() {
        count++;
    }


    void m2() {
        count2.incrementAndGet();
    }

    void m3() {
        count3.increment();
    }

    public static void main(String[] args) throws InterruptedException {
        T t = new T();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Thread thread = new Thread(t::m);
            thread.start();
            thread.join();
        }
        long end = System.currentTimeMillis();
        log.info("m sync 所耗用时间 {} ", (end - start));

        long start2 = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Thread thread = new Thread(t::m2);
            thread.start();
            thread.join();
        }
        long end2 = System.currentTimeMillis();
        log.info("m2 atomic 所耗用时间 {}", (end2 - start2));


        long start3 = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Thread thread = new Thread(t::m3);
            thread.start();
            thread.join();
        }
        long end3 = System.currentTimeMillis();
        log.info("m2 atomic 所耗用时间 {}", (end3 - start3));


    }


}
